package com.example.testapplication;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class ListViewDemo extends AppCompatActivity {
    private ListView mListView;
    @SuppressLint("ResourceType")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listviewdemo);

        mListView = (ListView) findViewById(R.id.list_view_demo);

        List<String> appNames = new ArrayList<>();
        appNames.add("QQ");
        appNames.add("微信");
        appNames.add("慕课网");

        mListView.setAdapter(new AppListAdpter(appNames));


    }

    public class AppListAdpter extends BaseAdapter{
        //这就是要填充的数据列表
        List<String> mAppNames;
        public AppListAdpter(List<String> appNames){
            mAppNames = appNames;
        }

        @Override
        public int getCount() {
            // 都有多少条数据
            return mAppNames.size();
        }

        @Override
        public Object getItem(int i) {
            // 获取当前i位置的这一条
            return null;
        }

        @Override
        public long getItemId(int i) {
            // 获取当前i位置的这一条id
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            // 处理 view --data 填充的数据
            LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.item_app_list_view,null);
            ImageView appIconImageView =(ImageView) view.findViewById(R.id.icon_image_view);
            TextView appNameTextView = (TextView) view.findViewById(R.id.app_name_image_view);

            appNameTextView.setText(mAppNames.get(i));

            return view;
        }
    }
}
